package com.itholmes.config;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

public class Watcher01 implements Watcher {

    @Autowired
    ServletContext servletContext;

    public ZooKeeper zk = null;

    @Override
    public void process(WatchedEvent event) {

        System.out.println("进入process---event = " + event);

        //这里对别后台使用
        List<String> children = new ArrayList<>();

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (event == null){
            return;
        }
        //连接状态
        Event.KeeperState keeperState = event.getState();
        //事件类型
        Event.EventType eventType = event.getType();
        //受影响的path
        String path = event.getPath();

        if (Event.KeeperState.SyncConnected== keeperState){
            //成功连接上ZK服务器
            if (Event.EventType.None == eventType){
                System.out.println("成功连接上ZK服务器");
                //拿到最新的zk数据
                try {
                    children = zk.getChildren("/provider", true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //创建节点
            else if (Event.EventType.NodeCreated == eventType){
                System.out.println("节点创建");
                try {
                    //zk.exists(path,true);
                    //zk.getChildren("/aa1",	true);

                    //拿到最新的zk数据
                    children = zk.getChildren("/provider", true);
                    System.out.println("节点创建");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //更新子节点
            else if (Event.EventType.NodeChildrenChanged == eventType){
                System.out.println("子节点变更");
                try {
                    //zk.exists(path,true);

                    //拿到最新的zk数据
                    children = zk.getChildren("/provider", true);
                    System.out.println("子节点变更");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //删除节点
            else if (Event.EventType.NodeDeleted == eventType){
                try {
                    //zk.exists(path,true);

                    //拿到最新的zk数据
                    children = zk.getChildren("/provider", true);
                    System.out.println("删除节点");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("节点 " + path + " 被删除");
            }else
                ;

            ArrayList<String> list_IP = new ArrayList<>();
            for (String child : children) {
                byte[] data = new byte[0];
                try {
                    data = zk.getData("/provider/" + child, true, null);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                list_IP.add(new String(data));
            }

            //查看获取到的IP
            for (Object o : list_IP) {
                System.out.println(o.toString());
            }

            IPSstatic.ips = list_IP;

            //通过随机数来获取
            //int i = (int)(Math.random() * (size - 1 - 0 + 1) + 0);
            //开始使用servletContext来传递参数，现在可以通过静态类来传递参数
            //servletContext.setAttribute("zk",list_IP);

        } else if (Event.KeeperState.Disconnected == keeperState) {
            System.out.println("与ZK服务器断开连接");
        } else if (Event.KeeperState.AuthFailed == keeperState) {
            System.out.println("权限检查失败");
        } else if (Event.KeeperState.Expired == keeperState) {
            System.out.println("会话失效");
        } else
            ;

        System.out.println("--------------------------------------------");

    }

}
